﻿using System;
using System.Collections.Generic;

namespace chudb.Utils
{
    internal class ArrayPool<T>
    {
        public static ArrayPool<T> Shared = new ArrayPool<T>();
        internal Dictionary<int, Queue<T[]>> map;
        public T[] Rent(int size)
        {
            if (!map.ContainsKey(size)) map[size] = new Queue<T[]>();

            if (map[size].Count == 0) return new T[size];
            return map[size].Dequeue();
        }
        public void Return(T[] array, bool clear)
        {
            var size = array.Length;
            if (!map.ContainsKey(size)) map[size] = new Queue<T[]>();

            map[size].Enqueue(array);
        }
    }
}
